#include "key.h"
#include "functions.h"
#include "lcd.h"

static uint8_t keyTrigger;
static uint8_t keyContinue;

void keyScan(void)
{
    uint8_t keyRead = KEY_PORT ^ 0xFF;    //异或，低电平（按键按下）则为 1，即读出按下的按键（设 0x01）
    
    //刚按下时，keyContinue 为 0x00，与 keyRead 异或后为 0x01，再与，keyTrigger=0x01
    //再次检测时：
    //如果没松开，则keyRead=0x01，keyContinue=0x01，异或后为 0，再与,keyTrigger=0x00
    //如果松开了，则keyRead=0x00，keyContinue=0x01，异或后为 0x01，再与，keyTrigger=0x00
    keyTrigger = keyRead & (keyRead ^ keyContinue);
    
    //对应按键为 0x01
    //（如果没松开），则keyRead=0x01，keyContinue=0x01
    //（如果松开了），则keyRead=0x00，keyContinue=0x00
    keyContinue = keyRead;
    
    /*
    所以：
    第一次按下：
            keyTrigger = 0x01;
            keyContinue = 0x01;
    长按：
            keyTrigger = 0x00;
            keyContinue = 0x01;
    */
    keyProcess(keyTrigger, keyContinue);
}

void keyProcess(uint8_t keyTrigger, uint8_t keyContinue)
{
    static uint8_t cnt = 0;
    static bit LongFlag = 0;
    
    if(BUZZER)
    {
        BUZZER = 0;     //关闭蜂鸣器
        return;
    }

    if(keyTrigger | keyContinue)    //有按键按下
    {
        exitMenuCount = 0;      //有按键按着，不返回主菜单
//        BUZZER = 0;
    }
    
    if(keyTrigger & KEY_MODE)       //Mode按键按下
    {
        //默认低位为0
        settingMode = settingMode & 0xF0;
        
        if((settingMode == 0x10) || (settingMode == 0x20))     //前面是设置 Alarm，保存 Alarm 设置
        {
            updateAlarms(alarmBuf);
        }
        else if(settingMode == 0x30)    //前面是设置 Time，保存 Time 设置
        {
            
        }
        
        settingMode += 0x10;
        if((settingMode == 0x10) || (settingMode == 0x20))
        {
            settingMode |= 0x01;    //0x11 OR 0x21
        }        
        else if(settingMode > 0x50)
        {
            settingMode = 0x00;
        }
        updateStatus2LCD(settingMode);
    }
    
    if(keyTrigger & KEY_SWITCH)     //切换按键按下
    {
        if((settingMode == 0x11) || (settingMode == 0x21))  //set Alarm
        {
            settingMode++;
        }
        else if((settingMode == 0x12) || (settingMode == 0x22))
        {
            settingMode--;
        }
        else if((settingMode & 0xF0) == 0x30) //set Time
        {
            settingMode += 2;
            if((settingMode & 0xF0) > 0x06)
            {
                settingMode = settingMode & 0xF0;
            }
        }
        updateStatus2LCD(settingMode);
    }
    
    if(keyTrigger & KEY_INCREASE)   //加按键按下
    {
        increaseTime(settingMode);
    }
    
    if(keyTrigger & KEY_DECREASE)   //减按键按下
    {
        decreaseTime(settingMode);
    }   

    if(keyContinue)                 //长按
    {
        cnt++;
        if(cnt >= 50)
        {
            LongFlag = 1;
            cnt = 0;
        }
        if(LongFlag && cnt > 6)
        {
            if(keyContinue & KEY_INCREASE)  //加长按
            {
                increaseTime(settingMode);
            }
            if(keyContinue & KEY_DECREASE)  //减长按
            {
                decreaseTime(settingMode);
            }
 
            cnt = 0;        
        }
    }
    else
    {
        cnt = 0;
        LongFlag = 0;
    }
}